<template>
  <div style="width: 500px">
    <el-input
        size="small"
        v-model="filterText"
        placeholder="请输入部门名称进行搜索...."
        prefix-icon="el-icon-search"></el-input>
    <el-tree
        :data="deps"
        node-key="id"
        :default-expand-all="true"
        :filter-node-method="filterNode"
        ref="tree"
        :props="defaultProps"
        >
      <span class="custom-tree-node" slot-scope="{node,data}"
          style="display: flex;justify-content: space-between;width: 100%">
        <span>{{data.name}}</span>
        <span>
          <el-button
              size="mini"
              class="depBtn"
              @click="()=>showAddDep(data)"
              type="primary">
            添加部门
          </el-button>
          <el-button
              type="danger"
              size="mini"
              class="depBtn"
              @click="()=>deleteDep(data)">
            删除部门
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
        title="添加部门"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>{{pname}}</td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input size="small" v-model="dep.name" placeholder="请输入部门名称"></el-input>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAddDep">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "DepMana",
  data(){
    return {
      deps: [],
      defaultProps:{
        children: 'children',
        label: 'name'
      },
      filterText:'',
      dialogVisible:false,
      dep:{
        name: '',
        parentId: -1,
      },
      pname: '',
    };
  },
  watch:{
    filterText(val){
      this.$refs.tree.filter(val);
    }
  },
  mounted() {
    this.initDeps();
  },
  methods:{
    initDep(){
        this.dep = {
          name: '',
          parentId: -1,
        };
    },
    // addDep2Deps(deps,dep){
    //   for (let i = 0; i < deps.length; i++){
    //     let d = deps[i];
    //     if (d.id === dep.parentId){
    //       d.children = d.children.concat(dep);
    //       return;
    //     }else {
    //       this.addDep2Deps(d.children,dep);
    //     }
    //   }
    // },
    /**
     * 添加业务网络请求
     */
    doAddDep(){
      this.postRequest('/system/basic/department/',this.dep).then(resp=>{
        if (resp){
          // this.addDep2Deps(this.deps, resp.obj);
          // 初始化
          this.initDeps();
          this.dialogVisible = false;
          this.initDep();
        }
      })
    },
    initDeps(){
      this.getRequest('/system/basic/department/').then(resp=>{
        if (resp){
          this.deps = resp;
        }
      })
    },
    /**
     * 添加部门
     * @param data
     */
    showAddDep(data){
      this.dep.parentId = data.id;
      this.pname = data.name;
      this.dialogVisible = true;
    },
    /**
     * 删除部门
     * @param data
     */
    deleteDep(data){
        this.$confirm(`此操作将永久删除该[${data.name}]部门，是否继续？`,'提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.deleteRequest(`/system/basic/department/${data.id}`).then(resp=>{
            if (resp){
              // this.removeDepFromDeps(this.deps, data.id);
              this.initDeps();
            }
          })
        }).catch(()=>{
          this.$message.info('取消操作')
        })
    },
    // removeDepFromDeps(deps,id){
    //   for (let i = 0; i< deps.length;i++){
    //     let d = deps[i];
    //     if (d.id === id){
    //
    //     }
    //   }
    // },
    /**
     *  用于检索
     * @param value 输入的搜索词
     * @param data 结点数据
     * @returns {boolean}
     */
    filterNode(value,data){
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
    },
  }
}
</script>

<style scoped>
.depBtn{
  padding: 2px;
}
</style>